import Vue from 'vue'
import router from './router'
import axios from 'axios'
// VueAxios 挂载axios
import VueAxios from 'vue-axios'
import VueLazyload from 'vue-lazyload'
import VueCookie from 'vue-cookie'
import { Message } from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import store from './store'
import App from './App.vue'
// import env from './env'

const mock = false;
// 调用真实接口时 mock关闭为false
// true 代表走的是mockjs本地模拟
if (mock) {
  /*require和import的区别
  import会预编译
  require执行到才会加载
   */
  require('./mock/api')
}
// 根据前端的跨域方式做调整
axios.defaults.baseURL = '/api';
//根据环境变量获取不同请求的地址
// axios.defaults.baseURL = env.baseURL;
// 接口超时限制5秒
axios.defaults.timeout = 5000;
// 接口错误拦截
axios.interceptors.response.use(function (response) {
  let res = response.data;
  res.status = 0;//关闭拦截
  let path = Location.hash;
  // 根据状态码 0成功 10未登录
  if (res.status === 0) {
    return res.data;
  } else if (res.status === 10) {
    if (path != '#/login') {
      window.location.href = '/#/login';
    }
    return Promise.reject();
  } else {
    this.$message.error(res.msg)
    // 抛出异常
    return Promise.reject();
  }
})
Vue.use(VueAxios, axios);
Vue.use(VueCookie)
// 图片懒加载图片引入
Vue.use(VueLazyload, {
  loading: '/imgs/loading-svg/loading-bars.svg',
});
// 扩展到原型
Vue.prototype.$message = Message;
Vue.config.productionTip = false

new Vue({
  store,
  router,
  render: h => h(App),
}).$mount('#app')
